define(['jquery', 'test'], function($, T) {
  function CheckCgiReturn(vdata, fcallback) {
    var errorcode = [
      "WEB_SUCCESS",
      "WEB_CONFIG_FAILURE",
      "WEB_NOT_LOGIN",
      "WEB_LOGIN_TIMEOUT",
      "WEB_DATA_ILLEGAL",
      "WEB_NO_PRIVILRGE",
      "WEB_INNER_ERROR",
      "ERROR_CODE_MAX"   
    ];

    var serrorCode = "errCode";
    var serrorMsg = "errMsg";

    if (typeof vdata == "object" && vdata[serrorCode] != undefined && vdata[serrorCode] != "0")
    {
      if (errorcode[vdata[serrorCode]] == "WEB_NOT_LOGIN")
        T.CDialog({title:"错误!", scontent:"您尚未登录，请先<a target='_top' href='/'>登录</a>", onlyone:true});
      else if (errorcode[vdata[serrorCode]] == "WEB_LOGIN_TIMEOUT")
        T.CDialog({title:"错误!", scontent:"登录超时，请重新<a target='_top' href='/'>登录</a>", onlyone:true});
      return false;
    }
    
    if (fcallback)
      fcallback();
    return true;
  }


  function PopCheckError(smsg, dom) {
    $("._dialog_dialog").remove();
    alert(smsg);
  }

  function Query(scgipath, orequestSet, fcallback, fcallbackError) {
    Request(scgipath, orequestSet, function(data) {
      if (CheckCgiReturn(data))
      {
        if (fcallback)
          fcallback(data);
      }
      else
      {
        if (fcallbackError)
          fcallbackError(data);
      }
    });
  }

  function Request(scgipath, vparam, fcallback) {
    var xmlhttp;
    if (window.XMLHttpRequest) // code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp = new XMLHttpRequest();
    else // code for IE6, IE5
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

    var url = scgipath; 
    if (T.isFunction(vparam))
    {
      fcallback = fcallback || vparam;
      vparam = null;
    }
    var url;
    if (!vparam)
      url = scgipath;
    else if (typeof vparam === "string")
    {
      url += "?" + vparam;
      vparam = null;
    }
    else if (typeof vparam === "object")
    {
      url += "?";
      for (var param in vparam)
      {
        var value = vparam[param];
        if (/[=;]/.test(value))
        {
          var dom = T.id(param);
          try
          {
            dom.select();
          }
          catch(e) {}
          PopCheckError("输入不能包含 = 和 ;");
          return;
        }
        url += param + "=" + encodeURIComponent(value) + "&";
      }
      url += "/=" + Math.random();
    }
    else
      url = scgipath;

    var jdata = null;
    xmlhttp.open("GET", url, false);
    try
    {
      xmlhttp.send();
    }
    catch(e)
    {
      PopCheckError("可能由于网络原因，您的请求未能发送，请检查连接后重试。");
      return;
    }
    var sresponse = xmlhttp.responseText;
    //var vReturnHeaders = xmlhttp.getAllResponseHeaders();
    var sreturnMIMEtype = xmlhttp.getResponseHeader("Content-Type");
    if (/json/.test(sreturnMIMEtype)) //判断返回类型
    {
      try
      {
        jdata = $.parseJSON(sresponse.replace(/\t/g, ""));
      }
      catch(e)//返回的不是正确的json字符串, 不能解析
      {
        return;
      }
    }
    if (fcallback)
      fcallback(jdata || sresponse, true);
  }

  return Query;
})